﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.ComponentModel;

namespace ReceiverSimulator
{
    public sealed class ReceiverSimulator : IDisposable
    {
        //Socket serverSocket;

        TcpListener serverSocket;

        public ReceiverSimulator()
        {
            //IPAddress ipAddress = IPAddress.Parse("192.168.0.249");
            //IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 60128);

            //serverSocket = new Socket(AddressFamily.InterNetwork,
            //    SocketType.Stream,
            //    ProtocolType.Tcp);
            //serverSocket.Bind(localEndPoint);
            //serverSocket.Listen(1);
            serverSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), 60128);
            serverSocket.Start();
            try
            {
                while (true)
                {
                    BackgroundWorker serverListener = new BackgroundWorker();
                    TcpClient client = null;

                    Console.WriteLine("ReceiverSimulator: Waiting for a connection...");

                    //client = serverSocket.Accept();
                    client = serverSocket.AcceptTcpClient();
                    serverListener.DoWork += (o, args) =>
                    {
                        Console.WriteLine("ReceiverSimulator: Connected.");

                        string data = null;
                        byte[] bytes = new byte[1024];
                        // An incoming connection needs to be processed.
                        while (true)
                        {
                            if (client.GetStream().CanRead)
                            {
                                bytes = new byte[1024];
                                int bytesRec = client.GetStream().Read(bytes, 0, bytes.Length);
                                data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                                Console.WriteLine("ReceiverSimulator: Received: " + data);
                                if (data.IndexOf("<EOF>") > -1)
                                {
                                    break;
                                }
                            }
                            //bytes = new byte[1024];
                            //int bytesRec = client.Receive(bytes);
                            //data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
                            //Console.WriteLine("ReceiverSimulator: Received: " + data);
                            //if (data.IndexOf("<EOF>") > -1)
                            //{
                            //    break;
                            //}
                        }
                    };
                    serverListener.RunWorkerCompleted += delegate(object obj, RunWorkerCompletedEventArgs args)
                    {
                        if (client != null)
                        {
                            client.Close();
                        }

                        //Console.WriteLine("ReceiverSimulator: Closed connection to " + client.RemoteEndPoint);
                        Console.WriteLine("ReceiverSimulator: Closed connection to " + client.Client.RemoteEndPoint);
                    };
                    serverListener.RunWorkerAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public void Dispose()
        {
            if (serverSocket != null)
            {
                //serverSocket.Close();
                serverSocket.Stop();
            }

            Console.WriteLine("ReceiverSimulator: Closed connection.");
        }
    }
}
